package com.alipay.service.redis;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Jun
 */
@Slf4j
@Service
public class BaseCacheService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 从Redis列表按顺序获取值
     * @param key
     * @return
     */
    public String getValue(String key){
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return redisTemplate.opsForList().leftPop(key);
    }

    /**
     * 向Redis插入列表
     * @param key
     * @param values
     */
    public void pushValue(String key, List<String> values){
        redisTemplate.opsForList().leftPushAll(key, values);
        redisTemplate.expire(key, 24, TimeUnit.HOURS);
    }
}
